package vn.sunnet.backviet01.game.coloa.store;

import vn.sunnet.backviet01.game.coloa.constant.Constants;
import vn.sunnet.util.security.InvalidChecksumException;
import vn.sunnet.util.security.SunnetPreferenceManager;
import android.content.Context;
import android.content.SharedPreferences;

public class GameStore {
	private SunnetPreferenceManager preferences;
	private SharedPreferences freePreferences;

	private int account;

	private int countRetry;
	private int stage;
	private int money;

	private int cTayTuyKinh;

	private int[] ID_Item_List_Store;
	private int[] ID_Skill_GeneralUsed;
	private int[] ID_Item_Used;

	private GeneralStore[] mGeneralUsed;
	private int mGeneralOpened;

	private int generalLastUsed;

	private boolean isLimitStage;
	private boolean isNewBie;
	
	private long mTime;

	private static final String PATH = "vn.sunnet.backviet.game.coloa";

	public GameStore(Context context, int account) {
		preferences = new SunnetPreferenceManager(context, PATH, "account"
				+ String.valueOf(account),
				SunnetPreferenceManager.SEC_ALGOR_DES);
		freePreferences = context.getSharedPreferences(PATH, 0);

		this.account = account;

		onLoadStep1(context);
	}

	public void onLoadStep1(Context context) {
		stage = this.freePreferences.getInt("account" + String.valueOf(account)
				+ "stage", 0);
		countRetry = freePreferences.getInt("account" + String.valueOf(account)
				+ "count_retry", 0);
		mTime = freePreferences.getLong("account" + String.valueOf(account)
				+ "time", 0);
		try {
			isNewBie = preferences.getBooleanValue("account" + String.valueOf(account)
					+ "new_bie", false);
		} catch (InvalidChecksumException e1) {
			e1.printStackTrace();
		}
		try {
			this.money = this.preferences.getIntValue(
					"account" + String.valueOf(account) + "money", 100000);
		} catch (InvalidChecksumException e) {
			e.printStackTrace();
		}
				
		mGeneralOpened = freePreferences.getInt(
				"account" + String.valueOf(account) + "opened", 1);

		loadGeneralLaseUsed();

		loadGeneral(context);
	}
	
	public void setRetry(int pRetry) {
		SharedPreferences.Editor editor = freePreferences.edit();
		countRetry = pRetry;
		
		editor.putInt("account" + String.valueOf(account)
				+ "count_retry", countRetry);
		
		editor.commit();
	}
	
	public int getRetry() {
		return countRetry;
	}
	
	public boolean isNewBie() {
		return isNewBie;
	}
	
	public void setNewBie(final boolean isNewBie) {
		this.isNewBie = isNewBie;
		
		preferences.saveValue("account" + String.valueOf(account)
				+ "new_bie", this.isNewBie);
	}
	
	public long getTime() {
		return mTime;
	}
	
	public void setTime(long time) {
		SharedPreferences.Editor editor = freePreferences.edit();
		mTime += time;
		editor.putLong("account" + String.valueOf(account)
				+ "time", mTime);
		editor.commit();
	}
	
	public void onLoadStep2(Context context) {

		cTayTuyKinh = freePreferences.getInt(
				"account" + String.valueOf(account) + "taytuykinh", 1);

		isLimitStage = freePreferences.getBoolean(
				"account" + String.valueOf(account) + "isLimitStage", false);

		for (int i = 0; i < mGeneralOpened; i++) {
			mGeneralUsed[i].onLoadStep2();
		}

		loadIDSkillGeneralUsed();

		loadItemUsed();
		loadIDItem();
		loadIDSkillGeneralUsed();
	}

	public void reset() {
		SharedPreferences.Editor editor = freePreferences.edit();
		stage = 0;
		editor.putInt("account" + String.valueOf(account) + "stage", 0);
		
		countRetry = 0;
		editor.putInt("account" + String.valueOf(account) + "count_retry", countRetry);

		cTayTuyKinh = 1;

		editor.putInt("account" + String.valueOf(account)
				+ "taytuykinh", 1);

		ID_Item_List_Store[0] = 8;
		editor.putInt(
				"account" + String.valueOf(account) + "id_item"
						+ String.valueOf(0), 8);
		
		mTime = 0;
		editor.putLong("account" + String.valueOf(account)
				+ "time", mTime);
		for (int i = 1; i < Constants.TOTAL_ITEM; i++) {

			ID_Item_List_Store[i] = -1;
			editor.putInt("account" + String.valueOf(account) + "id_item"
					+ String.valueOf(i), -1);

		}

		for (int i = 0; i < Constants.TOTAL_SKILL; i++) {
			ID_Skill_GeneralUsed[i] = -1;
			editor.putInt("account" + String.valueOf(account)
					+ "skill_general_used" + String.valueOf(i), -1);
		}

		ID_Item_Used[0] = Constants.TOTAL_SKILL;
		editor.putInt("account" + String.valueOf(account) + "item_used"
				+ String.valueOf(0), Constants.TOTAL_SKILL);

		for (int i = 1; i < 4; i++) {
			if (ID_Item_Used[i] != -1) {
				ID_Item_Used[i] = -1;
				editor.putInt("account" + String.valueOf(account) + "item_used"
						+ String.valueOf(i), -1);
			}

		}

		for (int i = 0; i < mGeneralOpened; i++) {
			try {
				mGeneralUsed[i].reset();
			} catch (InvalidChecksumException e) {
				e.printStackTrace();
			}
		}

		mGeneralOpened = 1;
		editor.putInt("account" + String.valueOf(account) + "opened", 1);

		generalLastUsed = Constants.DICHHUTONG;
		editor.putInt(
				"account" + String.valueOf(account) + "general_last_used",
				Constants.DICHHUTONG);

		editor.commit();
	}

	public boolean isLimitStage() {
		return isLimitStage;
	}

	public void setLimitStage(boolean isLimitStage) {
		SharedPreferences.Editor editor = freePreferences.edit();

		this.isLimitStage = isLimitStage;
		editor.putBoolean("account" + String.valueOf(account) + "isLimitStage",
				this.isLimitStage);
		editor.commit();
	}

	private void loadItemUsed() {
		ID_Item_Used = new int[4];

		ID_Item_Used[0] = freePreferences.getInt(
				"account" + String.valueOf(account) + "item_used"
						+ String.valueOf(0), Constants.TOTAL_SKILL);

		for (int i = 1; i < 4; i++) {

			ID_Item_Used[i] = freePreferences.getInt(
					"account" + String.valueOf(account) + "item_used"
							+ String.valueOf(i), -1);

		}
	}

	public void removeItemUsed(int index) {
		SharedPreferences.Editor editor = freePreferences.edit();

		ID_Item_Used[index] = -1;

		editor.putInt("account" + String.valueOf(account) + "item_used"
				+ String.valueOf(index), -1);
		editor.commit();

	}

	public int getIDItemUsed(int index) {
		return ID_Item_Used[index];
	}

	public void useItem(int index, int id) {
		SharedPreferences.Editor editor = freePreferences.edit();

		ID_Item_Used[index] = id;

		editor.putInt("account" + String.valueOf(account) + "item_used"
				+ String.valueOf(index), id);

		editor.commit();
	}

	public int getcTayTuyKinh() {
		return cTayTuyKinh;
	}

	public void useTayTuyKinh() {
		SharedPreferences.Editor editor = freePreferences.edit();
		this.cTayTuyKinh--;
		editor.putInt("account" + String.valueOf(account) + "taytuykinh",
				cTayTuyKinh);

		editor.commit();
	}

	public void buyTayTuyKinh(int more) {
		SharedPreferences.Editor editor = freePreferences.edit();
		this.cTayTuyKinh += more;
		editor.putInt("account" + String.valueOf(account) + "taytuykinh",
				cTayTuyKinh);
		editor.commit();
	}

	public void loadIDSkillGeneralUsed() {
		ID_Skill_GeneralUsed = new int[Constants.TOTAL_SKILL];

		for (int i = 0; i < Constants.TOTAL_SKILL; i++) {
			ID_Skill_GeneralUsed[i] = freePreferences.getInt(
					"account" + String.valueOf(account) + "skill_general_used"
							+ String.valueOf(i), -1);
		}

	}

	public int getIDSkillGeneralUsed(int ID) {
		try {
			return ID_Skill_GeneralUsed[ID];
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

	}

	public void setIDSkillGeneralUsed(int ID, int general) {
		SharedPreferences.Editor editor = freePreferences.edit();
		try {
			ID_Skill_GeneralUsed[ID] = general;
			editor.putInt("account" + String.valueOf(account)
					+ "skill_general_used" + String.valueOf(ID),
					ID_Skill_GeneralUsed[ID]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		editor.commit();
	}

	public int getmGeneralOpened() {
		return mGeneralOpened;
	}

	public void setmGeneralOpened(int mGeneralOpened) {
		SharedPreferences.Editor editor = freePreferences.edit();
		this.mGeneralOpened = mGeneralOpened;
		editor.putInt("account" + String.valueOf(account) + "opened",
				mGeneralOpened);
		editor.commit();
	}

	private void loadGeneral(Context context) {
		mGeneralUsed = new GeneralStore[4];

		for (int i = 0; i < mGeneralOpened; i++) {
			mGeneralUsed[i] = new GeneralStore(context, account, i);
		}
	}

	public GeneralStore getGeneral(int index) {
		return mGeneralUsed[index];
	}

	public void newGeneral(Context context) {
		SharedPreferences.Editor editor = freePreferences.edit();
		this.mGeneralOpened++;
		editor.putInt("account" + String.valueOf(account) + "opened",
				mGeneralOpened);

		mGeneralUsed[mGeneralOpened - 1] = new GeneralStore(context, account,
				mGeneralOpened - 1);
		editor.commit();
	}

	private void loadGeneralLaseUsed() {
		generalLastUsed = this.freePreferences.getInt(
				"account" + String.valueOf(account) + "general_last_used",
				Constants.DICHHUTONG);
	}

	public int getGeneralLastUsed() {
		return generalLastUsed;
	}

	public void updateGeneralLastUsed(int name) {
		SharedPreferences.Editor editor = freePreferences.edit();
		generalLastUsed = name;
		editor.putInt(
				"account" + String.valueOf(account) + "general_last_used", name);
		editor.commit();
	}

	private void loadIDItem() {
		ID_Item_List_Store = new int[Constants.TOTAL_ITEM];

		ID_Item_List_Store[0] = freePreferences.getInt(
				"account" + String.valueOf(account) + "id_item"
						+ String.valueOf(0), 8);

		for (int i = 1; i < Constants.TOTAL_ITEM; i++) {

			ID_Item_List_Store[i] = freePreferences.getInt(
					"account" + String.valueOf(account) + "id_item"
							+ String.valueOf(i), -1);

		}
	}

	public int getIDItem(int ID_Item) {
		return ID_Item_List_Store[ID_Item];
	}

	public void removeItem(int id) {
		SharedPreferences.Editor editor = freePreferences.edit();

		ID_Item_List_Store[id - Constants.TOTAL_SKILL] = -1;
		editor.putInt(
				"account" + String.valueOf(account) + "id_item"
						+ String.valueOf(id - Constants.TOTAL_SKILL), -1);

		editor.commit();
	}

	public void newItem(int newIdItem) {
		SharedPreferences.Editor editor = freePreferences.edit();

		ID_Item_List_Store[newIdItem - Constants.TOTAL_SKILL] = 1;
		editor.putInt(
				"account" + String.valueOf(account) + "id_item"
						+ String.valueOf(newIdItem - Constants.TOTAL_SKILL), 1);

		editor.commit();
	}

	public boolean pay(int price) {
		boolean isSuccess = false;
		
		if (money >= price) {
			isSuccess = true;
			this.money = money - price;
	
			preferences.saveValue("account" + String.valueOf(account) + "money",
					this.money);
		}
		
		return isSuccess;
	}

	public int getMoney() {
		return money;
	}

	public void buyGold(int _money) {
		this.money = money + _money;
		preferences.saveValue("account" + String.valueOf(account) + "money",
				this.money);
	}

	public void setCoin(int coin) {
		this.money = coin;
		preferences.saveValue("account" + String.valueOf(account) + "money",
				this.money);
	}

	public int getStage() {
		return stage;
	}

	public void setStage(final int stage) {
		SharedPreferences.Editor editor = freePreferences.edit();
		this.stage = stage;
		if (this.stage > 99)
			this.stage = 99;
		editor.putInt("account" + String.valueOf(account) + "stage", this.stage);
		editor.commit();
	}
}
